package com.beijiteshop.shop.ui.account.fragment

import android.os.Bundle
import android.view.View
import androidx.databinding.BindingBuildInfo
import com.beijiteshop.libcommon.ext.databinding.bindView
import com.beijiteshop.libcommon.utils.EventUtil
import com.beijiteshop.libcommon.view.CommonDialog
import com.beijiteshop.shop.R
import com.beijiteshop.shop.app.PageIndex
import com.beijiteshop.shop.base.BaseContainerFragment
import com.beijiteshop.shop.databinding.FragmentAccountBinding
import com.beijiteshop.shop.model.api.response.UserBindingInfoBean
import com.beijiteshop.shop.model.event.EventKey
import com.beijiteshop.shop.ui.login.vm.UserVM
import com.beijiteshop.shop.wxapi.WXHelper
import org.koin.android.viewmodel.ext.android.viewModel

/**
 * @author LiaoZhongKai
 * @Date 2021-03-25 17:23:45
 * @Desc 账号绑定页面
 */
class AccountFragment : BaseContainerFragment(R.layout.fragment_account) {

    private val mBinding by bindView<FragmentAccountBinding>()
    private val mUserVM: UserVM by viewModel()
    private var mBindingInfo: UserBindingInfoBean? = null


    override fun onLazyLoad() {
        requestData()
    }

    override fun initView(view: View) {

        mBinding.accountTitleBar.titleBarTitleTv.text = resources.getString(R.string.binding_account)

    }

    override fun initData() {

    }

    override fun initEvent() {

        startObserve()

        mBinding.accountTitleBar.titleBarBackIv.setOnClickListener {
            close()
        }

        //手机绑定
        mBinding.accountBindingPhoneCl.setOnClickListener {
            if (mBindingInfo != null && !mBindingInfo!!.phone.isNullOrEmpty()){
                showChangePhoneDialog(mBindingInfo!!.phone!!)
            }else{
                startActivity(PageIndex.BIND_PHONE, Bundle().apply {
                    putBoolean(BindPhoneFragment.TYPE_BIND, true)
                })
            }
        }

        //微信绑定
        mBinding.accountBindingWeChatCl.setOnClickListener {
            if (mBindingInfo != null
                && !mBindingInfo!!.wxName.isNullOrEmpty()
                && !mBindingInfo!!.headUrl.isNullOrEmpty()){//更换微信
                showChangeWXDialog(mBindingInfo!!.wxName!!)
            }else{
                bindWX()
            }
        }

        //账号绑定成功
        EventUtil.observe<Any>(this,EventKey.ACCOUNT_BIND_SUCCESS){
            requestData()
        }
    }

    private fun startObserve(){
        mUserVM.apply {

            liveBindingInfo.observeKt {bindingInfo ->
                mBindingInfo = bindingInfo
                hideLoading()
                if (bindingInfo != null){
                    mBinding.info = bindingInfo
                }
            }

            liveNetError.observeKt {
                hideLoading()
                showToast(it?:resources.getString(R.string.something_error))
            }

            liveError.observeKt {
                hideLoading()
                showToast(it?:resources.getString(R.string.something_error))
            }

            liveEmpty.observeKt {
                hideLoading()
            }
        }
    }

    private fun requestData(){
        showLoading()
        mUserVM.getBindingInfo()
    }

    private fun showChangeWXDialog(wxName: String){
        CommonDialog()
            .setTitle(resources.getString(R.string.change_bound_we_chat))
            .setContent(wxName)
            .setNegativeButton { _, commonDialog ->
                commonDialog.dismiss()
            }
            .setPositiveButton { _, commonDialog ->
                bindWX()
                commonDialog.dismiss()
            }
            .show(childFragmentManager,"")
    }

    private fun bindWX(){
        if (WXHelper.getInstance().isWXInstalled()){
            WXHelper.getInstance().getAuth()
        }else{
            showToast(resources.getString(R.string.please_install_wx_app))
        }
    }

    private fun showChangePhoneDialog(phone: String){
        CommonDialog()
            .setTitle(resources.getString(R.string.change_bound_phone))
            .setContent(phone)
            .setNegativeButton { _, commonDialog ->
                commonDialog.dismiss()
            }
            .setPositiveButton { _, commonDialog ->
                startActivity(PageIndex.BIND_PHONE, Bundle().apply {
                    putBoolean(BindPhoneFragment.TYPE_BIND, false)
                })
                commonDialog.dismiss()
            }
            .show(childFragmentManager,"")
    }
}